/*
 * name:用户名 UserName
 * author:yanmu.wj@taobao.com
 * date:2011-11-24
 */
KISSY.add('UserName',function(S,Tip,PlaceHolder){
    var UserName = function(cfg){
        this.input = cfg.input && S.one(cfg.input);
        this.placeholder = cfg.placeholder && cfg.input ? PlaceHolder({
            input:cfg.input,
            placeholder:cfg.placeholder,
            blurCls:'ph_blur'
        }) : null;
        this.checkUrl = cfg.checkUrl || '';
        this.tip = cfg.tip && S.one(cfg.tip) ? Tip(cfg.tip) : null;

        //默认tip信息
        this.defaultTip = cfg.defaultTip || null;

        this.disabledMsg = S.isString(cfg.disabledMsg) ? cfg.disabledMsg : '用户名输入有误，请重新输入！';
        this.disabled = true;
    };
    var _UserName = function(cfg){
        return new UserName(cfg);
    };
    S.augment(UserName,{
        regex:{
            illegal:/\.\.|--|__|－|＿|※|▲|△|　| /,
            allNumber:/^\d+$/
        },
        init:function(){
            if(!this.input || !this.tip) return this;
            
            this.placeholder && this.placeholder.init();

            var self = this;
            this.input.on('keyup',function(){
                self.check();
            });

            return this;
        },
        check:function(){
            var self = this;

            //可用性校验
            if(this.checkAble()){
                self.disabled = false;
                //self.tip.ok();
                self.tip.hide();
            }else if(!S.trim(this.input.val()).length){
                self.disabled = true;
                self.resetTip();
            }else{
                self.disabled = true;
                self.tip.error(this.disabledMsg);
            }

            if(!this.checkUrl) return;

            S.io({
                url:self.checkUrl,
                data:{code:S.trim(self.input.val())},
                type:'post',
                dataType:'json',
                success:function(data){
                    if(data.success){
                        self.disabled = false;
                        self.tip.ok(data.msg || '');
                    }else{
                        self.disabled = true;
                        self.tip.error(data.msg || self.disabledMsg);
                    }
                }
            });
        },
        isAllNumber:function(){
            return !!this.input.val().match(this.regex.allNumber);
        },
        isIllegal:function(){
            return !!this.input.val().match(this.regex.illegal);
        },
        size:function(){
            return S.trim(this.input.val()).length;
        },
        matchSize:function(){
            return this.size()>=5 && this.size()<20;
        },
        checkAble:function(){
            return this.matchSize() && !this.isAllNumber() && !this.isIllegal();
        },
        resetTip:function(){
            if(!this.tip) return;
            if(this.defaultTip && this.defaultTip.type && this.defaultTip.msg){
                this.tip.change(this.defaultTip.type,this.defaultTip.msg);
            }else{
                this.tip.hide();
            }
        }
    });

    return _UserName;
},{
    requires:['Message','PlaceHolder']
});
